﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using FRL.DebtChased.Repository;
using FRL.DebtChased.Repository.ViewModels;
using FRL.DebtChased.Website;

namespace WebApplication.Finance
{

	/// <summary>
	/// Declaration of PopupSearchDatabase
	/// </summary>
	public partial class PopupSearchDatabase : System.Web.UI.Page
	{

		/////////////////////////////////////////////////////////////
		// Event handlers

		protected void Page_Load(object sender, EventArgs e)
		{

			// If this is a postback...
			if (this.IsPostBack)
			{

			}
			else
			{

				// Set controls
				string strAction = Request["act"];
				if (strAction == "search")
				{

					// Load controls from url
					txtDebtorCompName.Text = Request["dbcmp"];
					txtDebtorFRLRef.Text = Request["dbref"];
					txtDebtorCompName.Text = Request["dbcmp"];
					txtDebtorFirstName.Text = Request["dbfn"];
					txtDebtorSurname.Text = Request["dbsur"];
					txtDebtorEmail.Text = Request["dbem"];
					txtDebtorPostcode.Text = Request["dbpc"];
					txtDebtorTel.Text = Request["dbtl"];
					txtDebtorPrevPay.Text = Request["dbpp"];
					txtDebtorMin.Text = Request["dbmn"];
					txtDebtorMax.Text = Request["dbmx"];
					txtCliCompName.Text = Request["clcm"];
					txtCliContName.Text = Request["clcn"];
					txtCliRef.Text = Request["clrf"];
					chkIncludeLPCDA.Checked = (Request["inlp"] == "on");
					chkSearchIncludeClosed.Checked = (Request["incl"] == "on");

					// if at least one field is selected...
					if (_isThereEnoughForASearch())
					{

						// Search straight away
						butSearch_Click(null, null);

					}

				}

			}

		}

		protected void butSearch_Click(object sender, EventArgs e)
		{

			// Validate form
			this.Validate();
			if (!this.IsValid)
				return;

			// Hide the error panel
			pnlError.Visible = false;

			// If there is NOT enough for a search
			if (!_isThereEnoughForASearch())
			{

				// Update error and display
				lbErrorMsg.Text = "Not enough information.";
				pnlError.Visible = true;

			}
			else
			{

				try
				{

					// Load the search model
					using (DebtChasedEntities ent = new DebtChasedEntities())
					{

						// Loadup the model
						SearchResultsModel m = new SearchResultsModel();
						m.DebtorCompName = txtDebtorCompName.Text;
						m.DebtorFRLRef = txtDebtorFRLRef.Text;
						m.DebtorFirstName = txtDebtorFirstName.Text;
						m.DebtorSurname = txtDebtorSurname.Text;
						m.DebtorEmail = txtDebtorEmail.Text;
						m.DebtorPostcode = txtDebtorPostcode.Text;
						m.DebtorTel = txtDebtorTel.Text;
						m.DebtorPrevPay = txtDebtorPrevPay.Text;
						m.DebtorMin = txtDebtorMin.Text;
						m.DebtorMax = txtDebtorMax.Text;
						m.CliCompName = txtCliCompName.Text;
						m.CliContName = txtCliContName.Text;
						m.CliRef = txtCliRef.Text;
						m.IncludeLPCDA = chkIncludeLPCDA.Checked;
						m.IncludeClosedAccounts = chkIncludeLPCDA.Checked;
						m.Load(ent);

						// Bind results to the grid view
						gridResults.DataKeyNames = new string[1] { "AccountId" };
						gridResults.DataSource = m.Elements;
						gridResults.DataBind();

						// Make sure the results panel is visible
						pnlResults.Visible = true;

					}

				}
				catch (Exception x)
				{

					// Update error and display
					lbErrorMsg.Text = ErrorProcessing.HandleException(x,"search database",MySession.BindMySession().UserID);
					pnlError.Visible = true;

				}

			}

		}

		protected void butDebtChaser_Click(object sender, EventArgs e)
		{

			// Get my session
			MySession mySession = MySession.BindMySession();

			// Add accounts ids
			mySession.UniqueAddExtraAccountIdsForChaseSession(gridResults, "chkIncAccount");

			// Refresh the main page

		}

		/////////////////////////////////////////////////////////////
		// Operations

		private bool _isThereEnoughForASearch()
		{

			// Check text fields for string of at least two characters
			return (
				(txtDebtorCompName.Text.Length >= 2) ||
				(txtDebtorFRLRef.Text.Length >= 2) ||
				(txtDebtorCompName.Text.Length >= 2) ||
				(txtDebtorFirstName.Text.Length >= 2) ||
				(txtDebtorSurname.Text.Length >= 2) ||
				(txtDebtorEmail.Text.Length >= 2) ||
				(txtDebtorPostcode.Text.Length >= 2) ||
				(txtDebtorTel.Text.Length >= 2) ||
				(txtDebtorPrevPay.Text.Length >= 2) ||
				(txtDebtorMin.Text.Length >= 2) ||
				(txtDebtorMax.Text.Length >= 2) ||
				(txtCliCompName.Text.Length >= 2) ||
				(txtCliContName.Text.Length >= 2) ||
				(txtCliRef.Text.Length >= 2)
			);

		}

	}

}